<?php

// ==============================================================================
//
// This file is part of the WelSpeak.
//
// Create by Welfony Support <support@welfony.com>
// Copyright (c) 2012-2014 welfony.com
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// ==============================================================================

namespace Welfony\Welspeak\Repository;

use Welfony\Welspeak\Core\Enum\InviteStatus;

class InviteRepository
{

    public static function findInviteById($id)
    {
        global $app;

        $strSql = 'SELECT
                       I.InviteId,
                       I.InvitorId,
                       I.SocialExternalId,
                       I.SocialType,
                       I.Status,
                       I.CreatedDate
                   FROM Invite I
                   WHERE I.InviteId = ?
                   LIMIT 1';

        return $app->conn->fetchAssoc($strSql, array($id));
    }

    public static function findInviteByPIN($pin, $socialExternalId, $socialType)
    {
        global $app;

        $strSql = 'SELECT
                       I.InviteId,
                       I.InvitorId,
                       I.SocialExternalId,
                       I.SocialType,
                       I.Status,
                       I.PIN,
                       I.CreatedDate
                   FROM Invite I
                   WHERE I.PIN = ? AND I.SocialExternalId = ? AND I.SocialType = ?
                   LIMIT 1';

        return $app->conn->fetchAssoc($strSql, array($pin, $socialExternalId, $socialType));
    }

    public static function getAllInviteByUser($userId)
    {
        global $app;

        $strSql = "SELECT
                       MAX(I.InviteId) InviteId,
                       U.UserId,
                       U.Username,
                       U.AvatarUrl,
                       U.ProfileBackgroundUrl,
                       SI.ExternalId SocialExternalId,
                       SI.Type SocialType
                   FROM Invite I
                   INNER JOIN (SELECT S.ExternalId, S.Type
                               FROM Social S
                               WHERE S.UserId = ?) SU ON SU.ExternalId = I.SocialExternalId AND SU.Type = I.SocialType
                   INNER JOIN Users U ON U.UserId = I.InvitorId
                   INNER JOIN Social SI ON SI.UserId = U.UserId
                   WHERE I.InvitorId NOT IN (SELECT FI.InvitorId
                                             FROM Friend FI
                                             WHERE FI.UserId = ?
                                             UNION
                                             SELECT FU.UserId
                                             FROM Friend FU
                                             WHERE FU.InvitorId = ?) AND I.Status = ?
                   GROUP BY I.InvitorId
                   ORDER BY I.CreatedDate DESC";

        return $app->conn->fetchAll($strSql, array($userId, $userId, $userId, InviteStatus::Pending));
    }

    public static function save($data)
    {
        global $app;

        try {
            if ($app->conn->insert('Invite', $data)) {
                return $app->conn->lastInsertId();
            }
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }

        return false;
    }

    public static function update($inviteId, $data)
    {
        global $app;

        try {
            return $app->conn->update('Invite', $data, array('InviteId' => $inviteId));
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }
    }

    public static function removeByInvitorAndUser($invitorId, $userId)
    {
        global $app;

        $sql = "DELETE FROM Invite
                       WHERE (InvitorId = ? AND ? IN (SELECT U.UserId
                                                       FROM Users U
                                                       INNER JOIN Social S ON S.UserId = U.UserId
                                                       WHERE S.ExternalId = SocialExternalId AND
                                                             S.Type = SocialType))
                              OR
                             (InvitorId = ? AND ? IN (SELECT U.UserId
                                                       FROM Users U
                                                       INNER JOIN Social S ON S.UserId = U.UserId
                                                       WHERE S.ExternalId = SocialExternalId AND
                                                             S.Type = SocialType))";

        try {
            $app->conn->executeQuery($sql, array($inviteId, $userId, $userId, $inviteId));
            return true;
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }
    }

}